//
//  LimitTextFieldViewController.m
//  TotalDemo
//
//  Created by gujian on 16/4/7.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "LimitTextFieldViewController.h"

static const NSInteger kMaxTextFieldLength = 10;
static const NSInteger kMaxTextViewLength = 10;

@interface LimitTextFieldViewController()<UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic, weak) UITextField *textField;

@property (nonatomic, weak) UITextView *textView;

@end

@implementation LimitTextFieldViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubViews];
}

- (void)initSubViews
{
    [self initTextField];
    [self initTextView];
}

- (void)initTextField
{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH-2*20, 30)];
    textField.layer.borderWidth = 1.0/[UIScreen mainScreen].scale;
    textField.layer.borderColor = COLOR_THEME.CGColor;
    textField.layer.cornerRadius = 2.0;
    textField.layer.masksToBounds = YES;
    textField.delegate = self;
    textField.placeholder = @"请输入您的姓名";
    textField.font = [UIFont systemFontOfSize:14.0];
    [self.view addSubview:textField];
    _textField = textField;
    
    //可防止无限联想输入
    //    [_textField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
}

- (void)initTextView
{
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 70, SCREEN_WIDTH-2*20, 80)];
    textView.layer.borderWidth = 1.0/[UIScreen mainScreen].scale;
    textView.layer.borderColor = COLOR_THEME.CGColor;
    textView.layer.cornerRadius = 2.0;
    textView.layer.masksToBounds = YES;
    textView.delegate = self;
    textView.font = [UIFont systemFontOfSize:14.0];
    [self.view addSubview:textView];
    _textView = textView;
    
//    //可防止无限联想输入
//        [_textField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark - UITextField delegate


// Method1 无法防止联想输入、无限输入
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *changedStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (!string.length) {
        return YES;
    }
    
    
    if ([textField.text length] > kMaxTextFieldLength) {
        return NO;
    }
    
    //防止无限输入,中文除外
    if (!textField.markedTextRange && changedStr.length > kMaxTextFieldLength) {
        textField.text = [changedStr substringToIndex:kMaxTextFieldLength];
        return  NO;
    }
    
    
    return YES;
    
    
    //    NSString *lang = [textField textInputMode].primaryLanguage;//[UITextInputMode currentInputMode];
    //    if ([lang isEqualToString:@"zh-Hans"]) {
    //        //不计算拼音在内
    //        UITextRange *selectedRange = [textField markedTextRange];
    //        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    //
    //        if (!position) {
    //
    //        }else
    //        {
    //            NSString *newStr = [textField textInRange:selectedRange];
    //            LotLog(@"%@",newStr);
    //        }
    //        
    //    }
}


//// Method2 可防止无限联想输入
//- (void)textFieldDidChanged:(UITextField *)textField
//{
//    NSString *currentStr = textField.text;
//    
//    if (textField.markedTextRange == nil && currentStr.length > kMaxTextFieldLength) {
//        textField.text = [currentStr substringToIndex:kMaxTextFieldLength];
//        [textField.undoManager removeAllActions];
//    }
//}


#pragma mark - UITextView delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *changedStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    if (!text.length) {
        return YES;
    }
    
    if (!textView.markedTextRange && changedStr.length > kMaxTextViewLength) {
        return NO;
    }
    
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *currentStr = textView.text;
    
    if (textView.markedTextRange == nil && currentStr.length > kMaxTextViewLength) {
        
        textView.text = [currentStr substringToIndex:kMaxTextViewLength];
        [textView.undoManager removeAllActions];
    }
}




@end
